package com.lu.servlet;

import com.lu.dao.impl.NewsDaoImpl;
import com.lu.entity.News;
import com.lu.entity.Page;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class GetSomeNewsByPageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();

        int total = new NewsDaoImpl().getCountNews();
        int count = 10;
        Page p = new Page(total, count);
        if (req.getParameter("currentPage") != null) {
            int param = Integer.parseInt(req.getParameter("currentPage"));
            p.setCurrentPage(param);
        }
        List<News> listNews = new NewsDaoImpl().getSomeNews(p);
        req.setAttribute("listNews", listNews);
        req.setAttribute("p",p);
        req.getRequestDispatcher("admin.jsp").forward(req,resp);

        out.flush();
        out.close();
    }
}
